package com.zimmur.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.fabric.xmlrpc.base.Array;
import com.zimmur.common.ReturnJsonEntity;
import com.zimmur.common.SpringBeanFactory;
import com.zimmur.common.StatusCodeEnum;
import com.zimmur.model.account.view.ConcernView;
import com.zimmur.model.base.AdBanner;
import com.zimmur.model.desiger.DesigerFav;
import com.zimmur.model.desiger.DesigerVoView;
import com.zimmur.model.desiger.view.DesigerInfoVo;
import com.zimmur.model.desiger.view.DesigerVo;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsCollect;
import com.zimmur.model.goods.GoodsParameter;
import com.zimmur.model.goods.GoodsType;
import com.zimmur.model.goods.view.GoodsTypeView;
import com.zimmur.model.order.WishList;
import com.zimmur.model.zone.Topic;
import com.zimmur.service.IMakerService;
import com.zimmur.service.IUserRecommandService;
import com.zimmur.service.IUserService;
import com.zimmur.service.base.IAdBannerService;
import com.zimmur.service.base.UserBrowserHistoryService;
import com.zimmur.service.designer.IDesignerFavService;
import com.zimmur.service.designer.IDesignerInfoService;
import com.zimmur.service.goods.IGoodsImageService;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.goods.IGoodsSizeService;
import com.zimmur.service.goods.IGoodsTypeService;
import com.zimmur.service.order.IWishListService;
import com.zimmur.service.zone.ITopicConcernService;
import com.zimmur.service.zone.ITopicService;
import com.zimmur.solr.model.Designer;
import com.zimmur.solr.service.ISolrGoodsService;
import com.zimmur.tools.DateUtils;
import com.zimmur.tools.PageUtil;


@RestController  
@RequestMapping("/restful/maker")  
public class MakerController extends BaseController {  
	private static Logger log=LoggerFactory.getLogger(MakerController.class);
	 @Resource  
	 private IGoodsService goodsService;
	 @Resource  
	 private IGoodsImageService goodsImageService; 
	 @Resource  
	 private IGoodsSizeService goodsSizeService;
	 @Resource  
	 private IMakerService makeService;
	 @Resource  
	 private IUserService userService;
	 @Resource  
	 private IUserRecommandService userRecommandService;
	 @Resource  
	 private ITopicConcernService topicConcernService;
	 @Resource
	 private IGoodsTypeService goodsTypeService;
	 @Resource
	 private IWishListService wishListService;
	 @Resource
	 private ITopicService topicService;
	 @Resource
	 private IDesignerFavService designerFavService;
	 @Resource  
	 private IDesignerInfoService designerInfoService;
	 @Resource
	 private IAdBannerService abBannerService;
	 @Resource
	 private ISolrGoodsService solrGoodsService;
	 
	 @Value("#{spingMvcProperties['System.GuestId']}")
	 private String guessUserId;
	 
	 /**
	 * 显示所有类别
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/allgoodstype",method=RequestMethod.GET)  
     public ReturnJsonEntity allGoodsType(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			//Short typeFlag=1;//类别：1=衣匠/2=家定
			Short typeFlag = getShortParam("typeFlag");
			Integer typeId = getIntParam("typeId");
			GoodsParameter goodsParameter = new GoodsParameter();
			goodsParameter.setTypeId(typeId);
			goodsParameter.setTypeFlag(typeFlag);
			
			List<GoodsType> goodsTypeList = makeService.allGoodsType(goodsParameter);//(typeFlag);
			entity.setData(goodsTypeList);
			
			statusCodeEnum = StatusCodeEnum.CODE000000;
 		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
     }
	 /**
	 * <p>Title: goodsTypeByflag</p>
	 * <p>Description: </p>
	 * @param request
	 * @param model
	 * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="/getalltypebyflag",method=RequestMethod.GET)  
	 public ReturnJsonEntity goodsTypeByflag(HttpServletRequest request,Model model){
		 ReturnJsonEntity entity = new ReturnJsonEntity();
		 StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		 try {
			 //Short typeFlag=1;//类别：1=衣匠/2=家定/3=配饰
			 Short typeFlag = getShortParam("typeFlag");
			 List<GoodsTypeView> goodsTypeList = goodsTypeService.selectAllTypeByFlag(typeFlag);
			 entity.setData(goodsTypeList);
			 
			 statusCodeEnum = StatusCodeEnum.CODE000000;
		 } catch (Exception e) {
			 log.error(e.getMessage());
			 statusCodeEnum=StatusCodeEnum.CODE100000;
			 entity.setData(new Array());
		 }
		 entity.setCode(statusCodeEnum.getCode());
		 entity.setMsg(statusCodeEnum.getMsg());
		 return entity;
	 }
	 /**
	 * 查询商品
	 * @param request
	 * @param model
	 * @return
	 */
	 @ResponseBody
     @RequestMapping(value="/goodsquery",method=RequestMethod.POST)  
     public ReturnJsonEntity goodsQuery(HttpServletRequest request,Model model){
		 ReturnJsonEntity entity = new ReturnJsonEntity();
			StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
			try {
				long userId= getTokenUserId();
				Integer pageNow = getIntParam("pageNow");
				String title = getParam("key");
				Integer typeId = getParam("typeId",0);
				//分类类别（按1衣匠2家定）
				Short typeFlag = getShortParam("typeFlag");
				// 排序条件
				String order=getParam("order");
				String clause=getParam("clause");
				Integer parentTypeId = 0;
				if(typeFlag == 0){
					typeFlag = 1;
				}
				GoodsParameter goodsParameter = new GoodsParameter();
				goodsParameter.setTypeFlag(typeFlag);
				List<String> typeIds=new ArrayList<>();
				if(typeId>0){
					typeIds.add(typeId.toString());
					GoodsType goodsType = goodsTypeService.selectByPrimaryId(typeId);
					if(goodsType != null){
						//typeFlag = goodsType.getTypeFlag();
						parentTypeId = goodsType.getParentTypeId();
						goodsParameter.setTypeId(typeId);
						if(parentTypeId==0){
							List<GoodsType> goodsTypes=goodsTypeService.selectByParentTypeIdGoodsType(goodsParameter);
							if(goodsTypes!=null && goodsTypes.size()>0){
								for(GoodsType type:goodsTypes){
									typeIds.add(type.getTypeId().toString());
								}
							}
						}
					}
					
				}
				List<SortClause> orderByClause=new ArrayList<>();
				
				if(order!=null && !order.isEmpty() && clause!=null && !clause.isEmpty()){
					if((order.equals("asc")||order.equals("desc"))){
						if(clause.equals("price")){
							orderByClause.add(new SortClause("price",order.equals("asc")?ORDER.asc:ORDER.desc));
						}else{
							orderByClause.add(new SortClause("saleNum",order.equals("asc")?ORDER.asc:ORDER.desc));
						}
					}
				}else{
					orderByClause.add(new SortClause("createDate",ORDER.desc));
				}
				
				if (title!=null && !"".equals(title)) {
					goodsParameter.setTitle(title);
				}
				goodsParameter.setTypeIds(typeIds);
				goodsParameter.setOrderByClause(orderByClause);
//				PageUtil<Goods> page = makeService.goodsQuery(userId,goodsParameter,pageNow);
				PageUtil<com.zimmur.solr.model.Goods> page = makeService.goodsQueryBySolr(userId,goodsParameter,pageNow);
				
			   	entity.setData(page);
			   	statusCodeEnum = StatusCodeEnum.CODE000000;
			} catch (Exception e) {
				log.error(e.getMessage());
				statusCodeEnum=StatusCodeEnum.CODE100000;
				entity.setData(new Array());
			} 
		    entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
     }
	 /**
	 * <p>Title: goodsList</p>
	 * <p>Description: 推荐广告商品列表查询</p>
	 * @param request
	 * @param model
	 * @return
	  */
	 @ResponseBody
	 @RequestMapping(value="/goodslist",method=RequestMethod.GET)  
	 public ReturnJsonEntity goodsList(HttpServletRequest request,Model model){
		 ReturnJsonEntity entity = new ReturnJsonEntity();
		 StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		 Boolean isUser = false ;
		 long userId= getTokenUserId();
		 String returnTitle = "";
		 try {
			 if (!String.valueOf(getTokenUserId()).equals(guessUserId)) {
					isUser = true;
				}
			 Integer markType =getParam("markType",0);//1=推荐广告搜索，2=商品类型搜索，3=搜索框搜索
			 Integer pageNow = getParam("pageNow",1);//当前页码
			 Integer pageSize = getParam("pageSize",10);//当前页码
			 String dataTypeIds = getParam("dataTypeId");//当markType为1时，推荐广告id;为2时，商品分类id
			 String offPercent=getParam("offPercent");//折扣
			 String goodColorName=getParam("goodColorName");//折扣
			 String price=getParam("price");//折扣
			 String goodSizeCode=getParam("goodSizeCode");//尺码
			 String keyword=getParam("keyword");
			 String tags = "";
			 // 排序条件
			 String order=getParam("order");
			 String clause=getParam("clause");
			 //分类类别（按1衣匠2家定）
			 Integer typeFlag = getIntParam("typeFlag");
			 PageUtil<com.zimmur.solr.model.Goods> GoodsList=new PageUtil<>();
			 AdBanner ad = null;
			 List<String> typeIds = new ArrayList<>();
			 
			 if(typeFlag == 0){
				 typeFlag = 1;
			 }
			 List<SortClause> orderByClause=new ArrayList<>();
			 if(order!=null && !order.isEmpty() && clause!=null && !clause.isEmpty()){
				 if((order.equals("asc")||order.equals("desc"))){
					 if(clause.equals("price")){
						 orderByClause.add(new SortClause("price",order.equals("asc")?ORDER.asc:ORDER.desc));
					 }else{
						 orderByClause.add(new SortClause("saleNum",order.equals("asc")?ORDER.asc:ORDER.desc));
					 }
				 }
			 }else{
				 orderByClause.add(new SortClause("createDate",ORDER.desc));
			 }
			 if(markType==1){
				 Integer typeid = Integer.valueOf(dataTypeIds);
				 //推荐广告查询
				 ad = abBannerService.getBannerByAdId(typeid);
				 if(ad!=null){
					 keyword ="";
					 tags= ad.getUrl();
				 }
				 returnTitle = ad.getTitle();
			 }else if(markType==2){
				 //商品类型搜索
				 keyword = "";
				 if(dataTypeIds.indexOf(",")<=0){
					 GoodsType goodsType = goodsTypeService.selectByPrimaryId(Integer.valueOf(dataTypeIds));
					if (goodsType != null) {
						if (goodsType.getParentTypeId().equals(0)) {
							List<GoodsType> goodsTypeList = getAllSubType(goodsType);
							for (GoodsType item : goodsTypeList) {
								typeIds.add(item.getTypeId() + "");
							}
						} else {
							typeIds.add(dataTypeIds);
						}
						returnTitle =goodsType.getTypeName();
					}else{
						typeIds.add(dataTypeIds);
					}
					 
				 }else{
					 if(!StringUtils.isEmpty(dataTypeIds)){
						 typeIds = Arrays.asList(dataTypeIds.split(","));
					 }
				 }
				 
			 }else{
				 returnTitle="搜索"+'"'+keyword+'"';
				 typeIds.clear();
			 }
			 GoodsList = solrGoodsService.queryGoods(typeIds, keyword, tags, typeFlag, orderByClause, 
					 offPercent, goodSizeCode, goodColorName, price,pageSize, pageNow);
			 //判断当前商品是否当前用户已加入的愿望单
			 if(isUser){
				 if(GoodsList.getItemList()!=null&&!GoodsList.getItemList().isEmpty()){
					 for(com.zimmur.solr.model.Goods g:GoodsList.getItemList()){
						 if(isUser){
								WishList wl  = wishListService.selectByGoodsIdAndUserId(userId, Long.parseLong(g.getGoodsId()));
								if(wl!=null){
									g.setIsWish(1);
								}
							}
					 }
				}
			 }
			 //设置返回的title
			 GoodsList.setTitle(returnTitle);
			 
			 entity.setData(GoodsList);
			 statusCodeEnum = StatusCodeEnum.CODE000000;
		 } catch (Exception e) {
			 log.error(e.getMessage());
			 statusCodeEnum=StatusCodeEnum.CODE100000;
			 entity.setData(new Array());
		 } 
		 entity.setCode(statusCodeEnum.getCode());
		 entity.setMsg(statusCodeEnum.getMsg());
		 return entity;
	 }
	 /**
	  * 获取该商品所有子类
	 * @Title: getAllSubType 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param dataTypeIds
	 * @return  
	 * @return List<String>    返回类型
	  */
	 private List<GoodsType> getAllSubType(GoodsType dataType) {
		GoodsParameter goodsParameter = new GoodsParameter();
		goodsParameter.setTypeId(dataType.getTypeId());
		goodsParameter.setTypeFlag(dataType.getTypeFlag());
		return goodsTypeService.selectByParentTypeIdGoodsType(goodsParameter );
	}
	/**
	 * 衣匠首页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/index",method=RequestMethod.GET)  
    public ReturnJsonEntity makerIndex(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			Integer pageNow = 1;
			long createUser= getTokenUserId();
			
			Map<String, Object> Index = makeService.MakerIndex(createUser,pageNow);
			
			entity.setData(Index);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 衣匠首页  分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/indexPage",method=RequestMethod.GET)  
    public ReturnJsonEntity makerIndexPage(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			Integer pageNow = getIntParam("pageNow");
			long createUser= getTokenUserId();
			PageUtil<DesigerVo> page = makeService.MakerPageByKeyAndTags(null,null,createUser,pageNow);
			
			entity.setData(page);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 根据标签查找设计师
	 * @return
	 */
	@RequestMapping(value="/designerbytags",method=RequestMethod.POST)
	public ReturnJsonEntity designerByTags(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			Integer pageNow = getIntParam("pageNow");
			long createUser= getTokenUserId();
			String tags=getParam("tags");
			Map<String, Object> map=new HashMap<>();
			map.put("tags", tags);
			
			if ("全部".equals(tags)) {
				tags="";
			}
			
//			PageUtil<DesigerVo> desigerVos= makeService.MakerPageByKeyAndTags(null,tags,createUser,pageNow);
			PageUtil<Designer> desigerVos= makeService.MakerQueryWithKeyAndTagsBySolr(null, tags, createUser, pageNow);
			map.put("desigerVos", desigerVos);
			entity.setData(map);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		}catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
	}
	
	/**
	 * 衣匠查询
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/makerIndexquert",method=RequestMethod.POST)  
    public ReturnJsonEntity makerIndexQuery(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			Integer pageNow = getIntParam("pageNow");
			long createUser= getTokenUserId();
			String key=getParam("key");
			if (pageNow==0) {
				pageNow=1;
			}
			Map<String, Object> map=new HashMap<>();
//			PageUtil<DesigerVo> desigerVos = makeService.MakerPageByKeyAndTags(key,null,createUser,pageNow);
			PageUtil<Designer> desigerVos = makeService.MakerQueryWithKeyAndTagsBySolr(key, null, createUser, pageNow);
			
			map.put("key", key);
			map.put("desigerVos", desigerVos);
			entity.setData(map);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 商品类别  ————无文档
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/goodstype",method=RequestMethod.GET)  
    public ReturnJsonEntity goodsTypeSelect(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			Integer typeId = getIntParam("typeId");
			
			List<Goods> goodsList = makeService.goodsTypeSelect(typeId);
			
			entity.setData(goodsList);
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 衣匠的个人主页  
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/designerindex",method=RequestMethod.GET)  
    public ReturnJsonEntity designerIndex(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			long userId = getLongParam("userId");
			long createUser=getTokenUserId();
			Map<String ,Object> map=new HashMap<>();
			//个人资料
			DesigerInfoVo desigerInfoVo=designerInfoService.selectDesigerByUserId(userId, createUser);
			map.put("designInfo", desigerInfoVo);
			//作品
			PageUtil<Goods> goodsList = makeService.desigerGoodsPage("",userId,1);
			map.put("goodsList", goodsList);
			desigerInfoVo.setGoodsNum(goodsList.getTotalRecord());
			//话题
			PageUtil<Topic> topicList=topicService.selectTopicByCreateUser(userId,1,1);
			map.put("topicList", topicList);
			desigerInfoVo.setTopicNum(topicList.getTotalRecord());
			
			entity.setData(map);    
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 衣匠的所有作品 分页
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/designerGoods",method=RequestMethod.GET)  
    public ReturnJsonEntity designerGoods(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			long userId = getLongParam("userId");
			Integer pageNow = getIntParam("pageNow");
			//String key = getParam("key");
			PageUtil<Goods> page = makeService.desigerGoodsPage("",userId,pageNow);
		    entity.setData(page);
			
		    statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
	/**
	 * 衣匠的话题 分页 
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/designertopic",method=RequestMethod.GET)  
    public ReturnJsonEntity designertopic(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		try {
			long userId = getLongParam("userId");
			Integer pageNow = getIntParam("pageNow");			
			PageUtil<Topic> topics=topicService.selectTopicByCreateUser(userId,1, pageNow);
			entity.setData(topics);    
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
	/**
	 * 关注衣匠
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/concerndesigner",method=RequestMethod.POST)  
    public ReturnJsonEntity concernDesigner(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		//获取当前时间，转为INT存储
        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
		long userId = getLongParam("userId");
        long createUser= getTokenUserId();
      //游客身份验证
		if(String.valueOf(createUser).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
        try {
        	DesigerFav desigerFav=new DesigerFav();
        	desigerFav.setCreateUserId(createUser);
        	desigerFav.setUserId(userId);
        	desigerFav.setCreateDate(timeDate);;
        	
        	DesigerFav isExist=designerFavService.selectDesigerFav(createUser, userId);
	        Integer FollowNum=0;
	        Integer resut=0;
	        if(null == isExist){
			    resut= designerFavService.insertDesigerFav(desigerFav);
			    FollowNum = 1;
	        }
	        if(null !=  isExist && isExist.getStatus()==1){
	        	isExist.setStatus((short)0);
	        	resut= designerFavService.updateDesigerFav(isExist);
	        	FollowNum = -1;
	        }else if(null !=  isExist && isExist.getStatus()==0){
	        	isExist.setStatus((short)1);
	        	resut= designerFavService.updateDesigerFav(isExist);
	        	FollowNum = 1;
	        }
	        
	        boolean bool= userService.updateUserFollowNum(userId,FollowNum);
	        
	        if(resut > 0 && bool ==true){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
        } catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
	/**
	 * 关注的衣匠 
	 * @param request
	 * @param model
	 * @return
	 */
	/*@ResponseBody
    @RequestMapping(value="/concerndesignerIndex",method=RequestMethod.GET)  
    public ReturnJsonEntity concerndesignerIndex(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try {
			long createUser= getTokenUserId();
			
			Map<String , Object> map = designerFavService.selectConcernIndex(createUser);
			entity.setData(map);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }*/
	/**
	 * 关注的衣匠 
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/concerndesignerPage",method=RequestMethod.GET)  
    public ReturnJsonEntity concerndesignerPage(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try {
			Integer pageNow = getIntParam("pageNow");
			long createUser= getTokenUserId();
			PageUtil<ConcernView> page = designerFavService.selectDesigerByDesigerFav(createUser,pageNow);
			if (page==null) {
				page=new PageUtil<>();
			}
			entity.setData(page);  
			statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}    	  
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 分享,转发
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/goodsshare",method=RequestMethod.POST)  
    public ReturnJsonEntity goodsShare(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try{
	        //String share = getParam("share");
	        long goodsId = getLongParam("goodsId");
	 
	        Integer resut = makeService.updateGoodsShareNum(null, goodsId);
	        
	        if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	//        	entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage());
			statusCodeEnum=StatusCodeEnum.CODE100000;
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 商品明细
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/goodsdetail",method=RequestMethod.GET)  
    public ReturnJsonEntity goodsDetail(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		Boolean isUser = false;
		try{
			if (!String.valueOf(getTokenUserId()).equals(guessUserId)) {
				isUser = true;
			}
	        long goodsId = getLongParam("goodsId");
	        long userId= getTokenUserId();
	        Goods goods = makeService.goodsDetails(goodsId,userId,isUser);
	        
	        if(goods != null){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	        	entity.setData(goods);
	        }else{
	        	entity.setData(new Array());
	        	statusCodeEnum = StatusCodeEnum.CODE400404;
	        }
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new ArrayList<>());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	@ResponseBody
    @RequestMapping(value="/browserhistory",method=RequestMethod.GET)  
    public ReturnJsonEntity userGoodsHistory(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100104;
		Boolean isUser = false;
		try{
			long tokenUserId = getTokenUserId();
			if (!String.valueOf(tokenUserId).equals(guessUserId)) {
				isUser = true;
			}
			if(isUser){
				UserBrowserHistoryService bean = SpringBeanFactory.getBean(UserBrowserHistoryService.class);
				List<com.zimmur.solr.model.Goods> list = bean.queryHistoryByUserId(tokenUserId+"", 16);
				if(!list.isEmpty()){
		        	statusCodeEnum = StatusCodeEnum.CODE000000;
		        	entity.setData(list);
		        }
			}else{
				entity.setData(new Array());
	        	statusCodeEnum = StatusCodeEnum.CODE100104;
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
	}
	/**
	 * 收藏商品
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/collectgoods",method=RequestMethod.POST)  
    public ReturnJsonEntity collectGoods(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum = StatusCodeEnum.CODE100000;
		try{
			//游客身份验证
			if(String.valueOf(getTokenUserId()).equals(guessUserId)){
				statusCodeEnum=StatusCodeEnum.CODE100200;
				entity.setCode(statusCodeEnum.getCode());
				entity.setMsg(statusCodeEnum.getMsg());
				return entity;
			}
			
			//获取当前时间，转为INT存储
	        Integer timeDate =DateUtils.getUnixTimeByDate(DateUtils.getNow());
	        long goodsId = getLongParam("goodsId");
	        long userId= getTokenUserId();
	        GoodsCollect goodsCollect =new GoodsCollect();
	        goodsCollect.setCreateDate(timeDate);
	        goodsCollect.setUserId(userId);
	        goodsCollect.setGoodsId(goodsId);
	        goodsCollect.setUpdateDate(timeDate);
		    
		    Integer resut = makeService.collectGoods(goodsCollect);
	        
		    if(resut > 0){
	        	statusCodeEnum = StatusCodeEnum.CODE000000;
	//        	entity.setData(resut);
	        }else{
	        	statusCodeEnum = StatusCodeEnum.CODE100000;
	        }
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	/**
	 * 我收藏的商品
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/mycollectgoods",method=RequestMethod.GET)  
    public ReturnJsonEntity myCollectGoods(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		//游客身份验证
		if(String.valueOf(getTokenUserId()).equals(guessUserId)){
			statusCodeEnum=StatusCodeEnum.CODE100200;
			entity.setCode(statusCodeEnum.getCode());
			entity.setMsg(statusCodeEnum.getMsg());
			return entity;
		}
		try{
			long userId= getTokenUserId();
			Integer pageNow = getIntParam("pageNow");
			
			PageUtil<Goods> page = makeService.myCollectGoods(userId,pageNow);
	        
		    entity.setData(page);
	    	statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
	/**
	 * 衣匠列表
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/designerList",method=RequestMethod.GET)  
    public ReturnJsonEntity designerList(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		
		try{
			//Map<String, List<DesigerVo>> desigerList=designerInfoService.selectAll();
			List<DesigerVoView> desigerList=designerInfoService.selectAll();
		    entity.setData(desigerList);
	    	statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
	/**
	 * 商品类型列表
	 * @param request
	 * @param model
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value="/goodsTypeList",method=RequestMethod.GET)  
    public ReturnJsonEntity goodsTypeList(HttpServletRequest request,Model model){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum statusCodeEnum=StatusCodeEnum.CODE100000;
		
		try{
	        List<GoodsTypeView> typeViews=goodsTypeService.selectAllType();
		    entity.setData(typeViews);
	    	statusCodeEnum = StatusCodeEnum.CODE000000;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			statusCodeEnum=StatusCodeEnum.CODE100000;
			entity.setData(new Array());
		}
        entity.setCode(statusCodeEnum.getCode());
		entity.setMsg(statusCodeEnum.getMsg());
		return entity;
    }
	
}  